﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.SalesPointModules
{
    public class SalesPointEmployeeUpdaterRegister
    {
        UserProfile userProfile;
        public SalesPointEmployeeUpdaterRegister()
        {
            userProfile = Authority.Authority.GetUserProfile();
        }
        public NormalDataUpdater<SalesPointEmployeeModel> GetSalesPointEmployeeUpdater()
        {
            NormalDataUpdater<SalesPointEmployeeModel> systemDataUpdater = new NormalDataUpdater<SalesPointEmployeeModel>();
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += EmployeeValid;
            return systemDataUpdater;
        }

        private void SingletonValid(object sender, DataValidationEventArgs<SalesPointEmployeeModel> e)
        {
            SalesPointEmployeeFetcher SalesPointEmployeeFetcher = new SalesPointEmployeeFetcher();
            string message = "";
            SalesPointEmployeeModel SalesPointEmployee = SalesPointEmployeeFetcher.GetByEmployee(userProfile.SiteId, e.Insert.EmployeeId, out message);
            if (SalesPointEmployee == null)
                return;
            if (SalesPointEmployee.AutoId != e.Insert.AutoId && SalesPointEmployee.EmployeeId == e.Insert.EmployeeId)
            {
                e.Cancle = true;
                message = String.Format("员工[{0}]已经加入到其他门店", e.Insert.EmployeeId);
                return;
            }

        }
        private void EmployeeValid(object sender,DataValidationEventArgs<SalesPointEmployeeModel> e)
        {
            string message = "";
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            if(employeeFetcher.Get(e.Insert.Site,e.Insert.EmployeeId,out message)==null)
            {
                e.Cancle = true;
                message = String.Format("员工[{0}]不存在", e.Insert.EmployeeId);
                return;
            }
        }

    }
}
